<template>
  <div class="login">
    <!-- @click-left="$router.back()" -->
    <van-nav-bar @click-left="$router.back()">
      <template #left>
        <i class="iconfont iconbtn_nav_back"></i>
      </template>
    </van-nav-bar>
    <div class="form">
      <h3>你好，请登录</h3>
      <van-form ref="loginForm">
        <van-field
          name="mobile"
          center
          placeholder="请输入手机号码"
          left-icon="iconfont"
          icon-prefix="iconfont iconbianzu1"
          v-model="loginForm.mobile"
          :rules="loginRules.mobile"
        ></van-field>
        <van-field
          center
          placeholder="请输入短信验证码"
          left-icon="iconfont"
          icon-prefix="iconfont iconyanzhengma"
          v-model="loginForm.code"
          :rules="loginRules.code"
        >
          <template #button>
            <div
              class="code"
              @click="sendCode"
              v-if="totalTime === 6"
              :disabled="false"
            >
              发送验证码
            </div>
            <div class="code" disabled v-else>{{ totalTime }}秒后再试</div>
          </template>
        </van-field>
      </van-form>
      <p>登录既同意<span>《用户使用协议》</span>和<span>《隐私协议》</span></p>
    </div>
    <div class="btnLogin">
      <van-button class="submit" @click="submit">确认</van-button>
    </div>
  </div>
</template>

<script>
import { auCode } from '@/api/login.js'
export default {
  data () {
    return {
      redirect: '',
      interval: '',
      totalTime: 6,
      loginForm: {
        mobile: '18888881111',
        code: ''
      },
      loginRules: {
        mobile: [
          { required: true, message: '必填', trigger: 'onBlur' },
          {
            validator: value => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return reg.test(value)
            },
            message: '请正确输入手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'onChange' },
          {
            validator: value => {
              return value.length === 4
            },
            message: '请输入六位验证码',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    sendCode () {
      this.$refs.loginForm.validate(['mobile']).then(async () => {
        this.totalTime--
        this.interval = setInterval(() => {
          this.totalTime--
          if (this.totalTime <= 0) {
            clearInterval(this.interval)
            this.totalTime = 6
          }
        }, 1000)
        const res = await auCode({ mobile: this.loginForm.mobile })
        console.log(res)
        this.loginForm.code = res.data.data
      })
    },
    submit () {
      this.$refs.loginForm.validate().then(async () => {
        await this.$store.dispatch('user/actionsToken', this.loginForm)
        this.$toast.success('登录成功')

        this.$router.push('/main')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  /deep/ .van-nav-bar__left {
    padding-left: 0px;
    .iconbtn_nav_back {
      font-size: 40px;
    }
  }
  .form {
    padding: 40px 0px 40px 10px;
    .code {
      border: none;
      background-color: transparent;
      color: #50c6e5;
    }
    h3 {
      font-size: 18px;
    }
    p {
      font-size: 12px;
      span {
        color: #50c6e5;
      }
    }
  }
  .btnLogin {
    padding: 10px 10px;
    .submit {
      width: 100%;
      height: 44px;
      background: #e40137;
      border-radius: 22px;
      font-size: 16px;
      color: #ffff;
    }
  }
}
</style>
